﻿using System.Collections;
using Gp.Scripts.Core;
using MoreMountains.Tools;
using UnityEngine;
using UnityEngine.UI;

namespace Gp.Scripts.Core.Controller {
	public class StartScreen : MonoBehaviour {
		[SerializeField] private Button button;
		[SerializeField] private Button newGame;


		/// the level to load after the start screen
		[Tooltip("the level to load after the start screen")]
		public string[] nextLevels;

		[Header("Fades")]
		/// the duration of the fade from black at the start of the level
		[Tooltip("the duration of the fade from black at the start of the level")]
		public float FadeInDuration = 1f;

		/// the duration of the fade to black at the end of the level
		[Tooltip("the duration of the fade to black at the end of the level")]
		public float FadeOutDuration = 1f;

		/// the tween type to use to fade the startscreen in and out 
		[Tooltip("the tween type to use to fade the startscreen in and out ")]
		public MMTweenType Tween = new (MMTween.MMTweenCurve.EaseInOutCubic);


		[SerializeField] private SaveSelectPanelController saveSelectPanel;
		

		// [Header("Sound Settings Bindings")]
		// /// the switch used to turn the music on or off
		// [Tooltip("the switch used to turn the music on or off")]
		// public MMSwitch MusicSwitch;
		// /// the switch used to turn the SFX on or off
		// [Tooltip("the switch used to turn the SFX on or off")]
		// public MMSwitch SfxSwitch;


		private bool _isTriggered = false;
		/// <summary>
		/// Initialization
		/// </summary>
		protected virtual void Awake() {
			MMFadeOutEvent.Trigger(FadeInDuration, Tween);
			Cursor.visible = true;
			SceneManageSystem.MakeSurePersistentScene();
			button.onClick.AddListener(ButtonPressed);
			newGame.onClick.AddListener(NewGameButtonPressed);
		}

		private void NewGameButtonPressed() {
			if (_isTriggered) return;
			MMFadeInEvent.Trigger(FadeOutDuration, Tween);
			_isTriggered = true;
			
			StartCoroutine(LoadFirstLevel());
		}


		// /// <summary>
		// /// On Start, initializes the music and sfx switches
		// /// </summary>
		// protected async void Start()
		// {
		// 	await Task.Delay(1);
		// 	
		// 	if (MusicSwitch != null)
		// 	{
		// 		MusicSwitch.CurrentSwitchState = MMSoundManager.Instance.settingsSo.Settings.MusicOn ? MMSwitch.SwitchStates.Right : MMSwitch.SwitchStates.Left;
		// 		MusicSwitch.InitializeState ();
		// 	}
		//
		// 	if (SfxSwitch != null)
		// 	{
		// 		SfxSwitch.CurrentSwitchState = MMSoundManager.Instance.settingsSo.Settings.SfxOn ? MMSwitch.SwitchStates.Right : MMSwitch.SwitchStates.Left;
		// 		SfxSwitch.InitializeState ();
		// 	}
		// }


		/// <summary>
		/// What happens when the main button is pressed
		/// </summary>
		private void ButtonPressed() {
			saveSelectPanel.Show();
		}

		
		
		
		/// <summary>
		/// Loads the next level.
		/// </summary>
		/// <returns>The first level.</returns>
		protected virtual IEnumerator LoadFirstLevel() {
			yield return new WaitForSeconds(FadeOutDuration);
			SceneManageSystem.LoadScenes(nextLevels);
		}
	}
}